package com.ycu.jk16.mall.dao.provider;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.ORDERTABLE;
import static com.ycu.jk16.mall.util.common.AndroidMalConstant.ORDER_DETAIL_TABLE;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import com.ycu.jk16.mall.domain.Order;
import com.ycu.jk16.mall.domain.OrderDetail;

/**
 * 
 * @ClassName OrderDynaSqlProvider
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author 倪宗龙
 * @date 2018年12月18日 下午2:17:46
 * @version V1.0
 */
public class OrderDynaSqlProvider {
	// 带参数的查询
	public String selectOrderWithParams(Map<String, Object> params) {
		String sql = new SQL() {
			{
				Object condition;
				SELECT("*");
				FROM(ORDERTABLE);
				if (params.get("no") != null) {
					condition = (" no = " + (String) params.get("no")).toString();
					if (!condition.equals("")) {
						WHERE((String) condition);
					}
				}

				if (params.get("userId") != null) {
					condition = (" user_id = " + (Integer) params.get("userId")).toString();
					if (!condition.equals("")) {
						WHERE((String) condition);
					}
				}
			}
		}.toString();

		// 此处加入了分页限制，每页显示 = (页数-1)*每页显示数, 页数*每页显示数 -1
		if (params.get("pageNum") != null) {
			int pageNum = (Integer) params.get("pageNum");
			int pageSize = (Integer) params.get("pageSize");
			sql += " limit " + (pageNum - 1) * pageSize + "," + (pageNum * pageSize);
		}

		return sql;
	}

	// 动态插入
	public String insertOrder(@Param("order") Order order, @Param("orderDetails") List<OrderDetail> orderDetails,
			@Param("userId") Integer userId) {

		String detailSql = "";
		for (OrderDetail od : orderDetails) {
			detailSql += new SQL() {
				{
					if (od != null) {
						INSERT_INTO(ORDER_DETAIL_TABLE);
						// VALUES("数据库字段", "#{java属性名}");
						if (od.getOrder() != null) {
							VALUES("order_no", od.getOrder().getNo().toString());
						}
						if (od.getProduct() != null) {
							VALUES("product_id", od.getProduct().getId().toString());
						}
						if (od.getCurrentProductName() != null) {
							VALUES("currentproduct_name", "'"+od.getCurrentProductName()+"'");
						}
						if (od.getCurrentUnitPrice() != null) {
							VALUES("currentUnitPrice", od.getCurrentUnitPrice().toString());
						}
						if (od.getQuantity() != null) {
							VALUES("quantity", od.getQuantity().toString());
						}
					}
				}
			}.toString() + ";";
		}

		String orderSql = new SQL() {
			{
				INSERT_INTO(ORDERTABLE);
				// VALUES("数据库字段", "#{java属性名}");
				if (order.getUser() != null) {
					VALUES("user_id", "#{userId}");
				}
				if (order.getNo() != null && !order.getNo().equals("")) {
					VALUES("no", "#{order.no}");
				}
				if (order.getPayment() != null && order.getPayment().doubleValue() >= 0.0) {
					VALUES("payment", "#{order.payment}");
				}

				VALUES("status", "1");

				if (order.getShipping() != null) {
					VALUES("shipping_id", "#{order.shipping.id}");
				}
				if (order.getCreateTime() != null && !order.getCreateTime().equals("")) {
					VALUES("create_time", "#{order.createTime}");
				}
				if (order.getEndTime() != null && !order.getEndTime().equals("")) {
					VALUES("end_time", "#{order.endTime}");
				}
				if (order.getCloseTime() != null && !order.getCloseTime().equals("")) {
					VALUES("close_time", "#{order.closeTime}");
				}
			}
		}.toString();

		return orderSql + " ;" + detailSql;
	}

	public void insertDetail() {

	}
}
